// API配置管理工具
class ApiConfigManager {
  constructor() {
    this.availableApis = {
      local: {
        name: '本地接口',
        url: 'http://localhost:3005',
        description: '本地开发环境'
      },
      remote1: {
        name: '远程接口1',
        url: 'https://69f63df3.r9.cpolar.top',
        description: '新的远程接口'
      },
      remote2: {
        name: '远程接口2',
        url: 'https://1f47e7a6.r9.cpolar.top',
        description: 'HTTPS远程接口'
      },
      remote3: {
        name: '远程接口3',
        url: 'https://6c46076a.r9.cpolar.top',
        description: '原有远程接口'
      }
    };
  }

  // 获取当前使用的API配置
  getCurrentApi() {
    const customApiUrl = localStorage.getItem('custom_api_url');
    const useRemote = localStorage.getItem('use_remote_api') === 'true';
    const useNewRemote = localStorage.getItem('use_new_remote_api') === 'true';
    
    if (customApiUrl) {
      return {
        name: '自定义接口',
        url: customApiUrl,
        description: '用户自定义接口'
      };
    }
    
    if (useNewRemote) {
      return this.availableApis.remote2;
    }
    
    if (useRemote) {
      return this.availableApis.remote1;
    }
    
    return this.availableApis.local;
  }

  // 设置API配置
  setApiConfig(type, customUrl = null) {
    switch (type) {
      case 'local':
        localStorage.removeItem('use_remote_api');
        localStorage.removeItem('use_new_remote_api');
        localStorage.removeItem('custom_api_url');
        break;
      case 'remote1':
        localStorage.setItem('use_remote_api', 'true');
        localStorage.removeItem('use_new_remote_api');
        localStorage.removeItem('custom_api_url');
        break;
      case 'remote2':
        localStorage.removeItem('use_remote_api');
        localStorage.setItem('use_new_remote_api', 'true');
        localStorage.removeItem('custom_api_url');
        break;
      case 'custom':
        localStorage.removeItem('use_remote_api');
        localStorage.removeItem('use_new_remote_api');
        localStorage.setItem('custom_api_url', customUrl);
        break;
      default:
        throw new Error('不支持的API类型');
    }
  }

  // 获取所有可用的API配置
  getAvailableApis() {
    return this.availableApis;
  }

  // 测试API连接
  async testApiConnection(url) {
    try {
      const response = await fetch(`${url}/ltc/profile`, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json'
        },
        timeout: 5000
      });
      return response.ok;
    } catch (error) {
      console.error('API连接测试失败:', error);
      return false;
    }
  }
}

export default new ApiConfigManager(); 